package com.test.prolenders.view_prolenders.process_widget_prolenders;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.test.prolenders.R;
import com.test.prolenders.base_prolenders.BaseProcessWidgetManager_prolenders;
import com.test.prolenders.bean_prolenders.process_prolenders.ProcessInfo_Data_prolenders;
import com.test.prolenders.dialog_prolenders.AddressSelectDialog_Tanzan_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;


public class AddressPickViewManager_Tanzan_prolenders extends BaseProcessWidgetManager_prolenders {

    private TextView titleTv;
    private TextView inputTv;
    private View clickView;

    private String state, city;

    public AddressPickViewManager_Tanzan_prolenders(View view, ProcessInfo_Data_prolenders data, String page){
        super(view,data,page);
    }

    @Override
    protected void init(){

        titleTv = mView.findViewById(R.id.vps_tv_title);
        inputTv = mView.findViewById(R.id.vps_tv_input);
        clickView = mView.findViewById(R.id.vps_view_click);

        setTitle(mData.getTitle());
        inputTv.setHint(mData.getHint());

        clickView.setOnClickListener(v -> {

            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_CLICK","");

            new AddressSelectDialog_Tanzan_prolenders(mView.getContext(), inputTv, addressStrs -> {

                StringBuilder value = new StringBuilder();
                for (String address:addressStrs){
                    value.append(address);
                }

                HttpUtil_prolenders.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_CLICK",getInput());

                if (addressStrs.length<2) return;
                state = addressStrs[0];
                city = addressStrs[1];
            },page+"_"+mData.getParamName()).show();
        });

    }


    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return (!TextUtils.isEmpty(state))&&(!TextUtils.isEmpty(city));
    }

    @Override
    public String getInput() {
        String input = inputTv.getText().toString();
        if (TextUtils.isEmpty(input)) return "";
        return input;
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(GONE);
        else {
            titleTv.setVisibility(VISIBLE);
            titleTv.setText(title);
        }
    }

    public String getState() {
        if (TextUtils.isEmpty(state)) return "";
        return state;
    }

    public String getCity() {
        if (TextUtils.isEmpty(city)) return "";
        return city;
    }

}
